package com.example.mode.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.mode.R;
import com.example.mode.beam.WeatherModel;
import com.example.mode.utils.NetUtil;
import com.google.gson.Gson;

public class WeatherActivity extends AppCompatActivity {
    private AppCompatSpinner mSpinner;
    private ArrayAdapter<String> mSpinnerAdapter;
    private String[] mCities;
    private TextView tvWeather,tvTemp,tvTempLowHigh,tvWin,tvAir;
    private ImageView ivWeather;
    private RecyclerView futureWeather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        mSpinner = findViewById(R.id.sp_city);
        mCities = getResources().getStringArray(R.array.cities);
        mSpinnerAdapter = new ArrayAdapter<>(this, R.layout.sp_item, mCities);
        mSpinner.setAdapter(mSpinnerAdapter);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String selectedCity = mCities[i];
                getWeatherOfCity(selectedCity);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });

        tvWeather = findViewById(R.id.tv_weather);
        tvAir = findViewById(R.id.tv_air);
        tvTemp = findViewById(R.id.tv_tem);
        tvTempLowHigh = findViewById(R.id.tv_tem_low_high);
        tvWin = findViewById(R.id.tv_win);
        ivWeather = findViewById(R.id.iv_weather);
        futureWeather = findViewById(R.id.future_weather);
    }

    private void getWeatherOfCity(String selectedCity) {
// 开启子线程，请求网络数据
        new Thread(new Runnable() {
            @Override
            public void run() {
// 通过网络请求工具类实现天气数据获取
                String weatherOfCity = NetUtil.getWeatherOfCity(selectedCity);
// 使用handler消息传递机制将天气数据传递给主线程
                Message message = Message.obtain();
                message.what = 0;
                message.obj = weatherOfCity;
                mHandler.sendMessage(message);
            }
        }).start();
    }
    private Handler mHandler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                String weather = (String) msg.obj;
                Log.d("res", "主线程接收到了天气数据--weather-- " + weather);
                Gson gson = new Gson();
                WeatherModel weatherModel = gson.fromJson(weather, WeatherModel.class);
                Log.d("res", "解析后的天气数据--weather-- " + weatherModel);
                updateUiOfWeather(weatherModel);
            }

        }
    };
    private void updateUiOfWeather(WeatherModel weatherModel) {
        if (weatherModel == null) {
            return;
        }
        tvWeather.setText(weatherModel.getWea() + "(" + weatherModel.getDate() + weatherModel.getWeek() + ")");
        tvTemp.setText(weatherModel.getTem());
        tvTempLowHigh.setText(weatherModel.getTemLow() + "~" + weatherModel.getTemHigh());
        tvWin.setText(weatherModel.getWin() + weatherModel.getWinSpeed());
        tvAir.setText("空气:" + weatherModel.getAir());
        ivWeather.setImageResource(getImgResOfWeather(weatherModel.getWeaImg()));
    }
    private int getImgResOfWeather(String weaImg) {
        int result = 0;
        switch (weaImg) {
            case "qing":
                result = R.drawable.biz_plugin_weather_qing;
                break;
            case "yin":
                result = R.drawable.biz_plugin_weather_yin;
                break;
            case "yu":
                result = R.drawable.biz_plugin_weather_dayu;
                break;
            case "bingbao":
                result = R.drawable.biz_plugin_weather_leizhenyubingbao;
                break;
            case "yun":
                result = R.drawable.biz_plugin_weather_duoyun;
                break;
            case "wu":
                result = R.drawable.biz_plugin_weather_wu;
                break;
            case "shachen":
                result = R.drawable.biz_plugin_weather_shachenbao;
                break;
            case "lei":
                result = R.drawable.biz_plugin_weather_leizhenyu;
                break;
            case "xue":
                result = R.drawable.biz_plugin_weather_baoxue;
                break;
            default:
                result = R.drawable.biz_plugin_weather_qing;
                break;
        }
        return result;
    }

}